cmake_minimum_required(VERSION 3.10)

# 查找Google Test
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# 查找Redis库
find_library(HIREDIS_LIBRARY NAMES hiredis PATHS /usr/local/lib /usr/lib)

# 使用顶层统一配置的spdlog
if(NOT TARGET spdlog_unified)
    message(FATAL_ERROR "spdlog_unified not found!")
endif()

# 包含主项目头文件
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/../include
    ${CMAKE_CURRENT_SOURCE_DIR}/../handlers
    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../TraderManager/include
    # spdlog include dirs are provided by spdlog_unified target
)

# 创建RedisPool库（用于测试）
add_library(RedisPoolLib STATIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../../TraderManager/src/RedisPool.cpp
)
target_link_libraries(RedisPoolLib
    ${HIREDIS_LIBRARY}
    fmt
    pthread
)

# 测试辅助函数
add_library(TestHelpers STATIC test_helpers.cpp)
target_link_libraries(TestHelpers
    MessageFramework
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)

# ==================== 单元测试 ====================

# MessageHeader测试
add_executable(test_message_header unit/test_message_header.cpp)
target_link_libraries(test_message_header
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME MessageHeader COMMAND test_message_header)

# SignatureHelper测试
add_executable(test_signature_helper unit/test_signature_helper.cpp)
target_link_libraries(test_signature_helper
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME SignatureHelper COMMAND test_signature_helper)

# EncryptionHelper测试
add_executable(test_encryption_helper unit/test_encryption_helper.cpp)
target_link_libraries(test_encryption_helper
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME EncryptionHelper COMMAND test_encryption_helper)

# MessageDispatcher测试
add_executable(test_message_dispatcher unit/test_message_dispatcher.cpp)
target_link_libraries(test_message_dispatcher
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME MessageDispatcher COMMAND test_message_dispatcher)

# MessageProducer测试（需要Redis）
add_executable(test_message_producer unit/test_message_producer.cpp)
target_link_libraries(test_message_producer
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME MessageProducer COMMAND test_message_producer)

# MessageConsumer测试（需要Redis）
add_executable(test_message_consumer unit/test_message_consumer.cpp)
target_link_libraries(test_message_consumer
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME MessageConsumer COMMAND test_message_consumer)

# ==================== 集成测试 ====================

# 端到端测试
add_executable(test_e2e integration/test_e2e.cpp)
target_link_libraries(test_e2e
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME EndToEnd COMMAND test_e2e)

# 故障恢复测试
add_executable(test_fault_tolerance integration/test_fault_tolerance.cpp)
target_link_libraries(test_fault_tolerance
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)
add_test(NAME FaultTolerance COMMAND test_fault_tolerance)

# ==================== 性能测试 ====================

# 吞吐量测试
add_executable(test_throughput performance/test_throughput.cpp)
target_link_libraries(test_throughput
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)

# 延迟测试
add_executable(test_latency performance/test_latency.cpp)
target_link_libraries(test_latency
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)

# 并发测试
add_executable(test_concurrency performance/test_concurrency.cpp)
target_link_libraries(test_concurrency
    MessageFramework
    TestHelpers
    RedisPoolLib
    fmt
    ${GTEST_LIBRARIES}
    pthread
)

# 性能测试不自动运行，需要手动执行
# add_test(NAME Throughput COMMAND test_throughput)
# add_test(NAME Latency COMMAND test_latency)
# add_test(NAME Concurrency COMMAND test_concurrency)

message(STATUS "MessageFramework tests configured")
message(STATUS "  Unit tests: 6 (Header, Signature, Encryption, Dispatcher, Producer, Consumer)")
message(STATUS "  Integration tests: 2 (E2E, FaultTolerance)")
message(STATUS "  Performance tests: 3 (Throughput, Latency, Concurrency)")
message(STATUS "  Total test cases: 73 (62 unit + 10 integration + 1 performance)")
